package ModelLayer;
import java.util.*;


public class Statistics
{
    private ArrayList<Sale> sales;
    private ArrayList<Lease> leases;
    private ArrayList<Employee> employees;
    private ArrayList<Order> orders;
    private ArrayList<Product> products;
    private ArrayList<Customer> customers;
    
    public Statistics()
    {
        sales = SaleContainer.getInstance().getSaleList();
        leases = LeaseContainer.getInstance().getLeaseList();
        employees = EmployeeContainer.getInstance().getEmployeesList();
        orders = OrderContainer.getInstance().getOrderList();
        products = ProductContainer.getInstance().getProductsList();
        customers = CustomerContainer.getInstance().getCustomersList();
    }
    
    public void employeeStatistics(int employeeID)
    {
        System.out.println("Statistics for employee: " + EmployeeContainer.getInstance().findEmployeeById(employeeID).getId());
        System.out.println("Name: " + EmployeeContainer.getInstance().findEmployeeById(employeeID).getName());
        System.out.println("E-mail: " + EmployeeContainer.getInstance().findEmployeeById(employeeID).getEmail());
        System.out.println("Phone number: " + EmployeeContainer.getInstance().findEmployeeById(employeeID).getPhoneNumber());
        System.out.println("Department: " + EmployeeContainer.getInstance().findEmployeeById(employeeID).getDepartment());
        System.out.println("Sales completed: " + EmployeeContainer.getInstance().findEmployeeById(employeeID).getSold());
        System.out.println("Leases completed: " + EmployeeContainer.getInstance().findEmployeeById(employeeID).getLeased());
        System.out.println("Total generated money: " + EmployeeContainer.getInstance().findEmployeeById(employeeID).getIncome());
        System.out.println("Orders completed: " + EmployeeContainer.getInstance().findEmployeeById(employeeID).getOrdered());
    }
    
    public void employeesStatistics()
    {
        for(int i=0; i<employees.size(); i++) {
            if(employees.get(i) != null) {
                employeeStatistics(employees.get(i).getId());
            }
        }
    }
    
    public void productStatistics(int productBarcode)
    {
        System.out.println("Statistics for product: " + ProductContainer.getInstance().findProductByBarcode(productBarcode).getBarcode());
        System.out.println("Name: " + ProductContainer.getInstance().findProductByBarcode(productBarcode).getName());
        System.out.println("Sold: " + ProductContainer.getInstance().findProductByBarcode(productBarcode).getSold());
        System.out.println("Leased: " + ProductContainer.getInstance().findProductByBarcode(productBarcode).getLeased());
        System.out.println("Ordered: " + ProductContainer.getInstance().findProductByBarcode(productBarcode).getOrdered());
        
        double income = 0;
        income += ProductContainer.getInstance().findProductByBarcode(productBarcode).getSellingPrice() * ProductContainer.getInstance().findProductByBarcode(productBarcode).getSold();
        income += ProductContainer.getInstance().findProductByBarcode(productBarcode).getLeasingPrice() * ProductContainer.getInstance().findProductByBarcode(productBarcode).getLeased();
        
        double outcome = 0;
        outcome = ProductContainer.getInstance().findProductByBarcode(productBarcode).getPriceForDiscount() * ProductContainer.getInstance().findProductByBarcode(productBarcode).getOrdered();
        
        System.out.println("Total income: " + income);
        System.out.println("Total outcome: " + outcome);
        System.out.println("Total: " + (income - outcome));
    }
    
    public int bestSellerStatistics()
    {
        int sold = 0;
        Product pObject = null;
        for(int i=0; i<products.size(); i++) {
            if(products.get(i).getSold() > sold) {
                pObject = products.get(i);
            }
        }
        
        return pObject.getBarcode();
    }
    
    public int bestLeasserStatistics()
    {
        int leased = 0;
        Product pObject = null;
        for(int i=0; i<products.size(); i++) {
            if(products.get(i).getLeased() > leased) {
                pObject = products.get(i);
            }
        }
        
        return pObject.getBarcode();
    }
    
    public void customerStatistics(int id)
    {
        System.out.println("Statistics for customer: " + CustomerContainer.getInstance().findCustomerById(id).getId());
        System.out.println("Name: " + CustomerContainer.getInstance().findCustomerById(id).getName());
        System.out.println("E-mail: " + CustomerContainer.getInstance().findCustomerById(id).getEmail());
        System.out.println("Phone number: " + CustomerContainer.getInstance().findCustomerById(id).getPhoneNumber());
        System.out.println("Address: " + CustomerContainer.getInstance().findCustomerById(id).getAddress());
        System.out.println("Discount: " + CustomerContainer.getInstance().findCustomerById(id).getDiscount());
        System.out.println("Total products bought: " + CustomerContainer.getInstance().findCustomerById(id).getBought());
        System.out.println("Total products leased: " + CustomerContainer.getInstance().findCustomerById(id).getRented());
        System.out.println("Total money spent: " + CustomerContainer.getInstance().findCustomerById(id).getIncome());
    }
    
    public int mostSpendingCustomer()
    {
        double income = 0;
        Customer cObject = null;
        for(int i=0; i<customers.size(); i++) {
            if(customers.get(i).getIncome() > income) {
                cObject = customers.get(i);
            }
        }
        
        return cObject.getId();
    }
}
